package com.dkt.blogboot.service;

import com.dkt.blogboot.entity.Comment;
import com.dkt.blogboot.entity.CommentBlogVo;
import com.dkt.blogboot.entity.User;
import com.dkt.blogboot.mapper.CommentMapper;
import com.dkt.blogboot.mapper.UserMapper;
import com.dkt.blogboot.util.CommentUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.List;

@Service
@Transactional(rollbackFor = Exception.class)
public class CommentService {
    @Autowired(required = false)
    private CommentMapper commentMapper;

    @Autowired(required = false)
    private UserMapper userMapper;

    public List<Comment> getCommentList(Long aid) {
        List<Comment> list = commentMapper.getCommentList(aid);
        return CommentUtils.processComments(list);
    }

    public Long getCommentListTotal(Long id) {
        return commentMapper.getCommentListTotal(id);
    }

    public List<CommentBlogVo> getLatestComments() {
        return commentMapper.getLatestComments();
    }

    public boolean save(Comment comment) {
        //User user = userMapper.getUserById(comment.getUid());
        comment.setCreateTime(new Date());
        comment.setIsDelete(0);
        return commentMapper.save(comment) > 0;
    }
}
